package com.bing.bing.contro

import com.bing.bing.mapper.UserDao
import com.bing.bing.pojo.User
import com.bing.bing.servi.UserService
import spock.lang.Specification
import spock.lang.Timeout
import spock.lang.Unroll

import java.util.concurrent.TimeUnit


class UserServiceUnitTest extends Specification {
    UserService userService = new UserService()
    UserDao userDao = Mock(UserDao)

    def setup(){
        userService.userDao = userDao
    }

    def "test login with success"(){

        when:
        userService.login("k", "p")

        then:
        1 * userDao.findByName("k") >> new User("k", 12,"p")
    }

    def "test login with error"(){
        given:
        def name = "k"
        def passwd = "p"

        when:
        userService.login(name, passwd)

        then:
        1 * userDao.findByName(name) >> null

        then:
        def e = thrown(RuntimeException)
        e.message == "${name}存在"

    }

    @Unroll
    def "test login with "() {
        when:
        userService.login(name, passwd)

        then:
        userDao.findByName("k") >> null
        userDao.findByName("k1") >> new User("k1", 12, "p")

        then:
        def e = thrown(RuntimeException)
        e.message == errMsg

        where:
        name | passwd | errMsg
        "k"  | "k"    | "${name}不存在"
        "k1" | "p1"   | "${name}密码输入错误"

    }

    @Timeout(value = 900, unit = TimeUnit.MILLISECONDS)
    def "test timeout"(){
        expect:
        Thread.sleep(1000)
        1 == 1
    }

    def "test findByName by verity"() {
        given:
        def userDao = Mock(UserDao)

        when:
        userDao.findByName("kk") >> new User("kk", 12, "33")

        then:
        def user = userDao.findByName("kk")
        with(user) {
            name == "kk"
            age == 122
            passwd == "33"
        }

    }

    def "querySimpleBeanById_根据券Id查询simpleBean"(){
        given: "券Id"
        def id = "k"
        when:
        def bean = userDao.findByName(id)
        then:
        assert !bean
//        assert bean.getName()
//        bean == new User("k", 1, "123")

    }




}